<?php
if(!isset($_SESSION)){session_start();};
require_once("../../../core.php");

$per_page = 10;
$sqlc = $sql = "SELECT
				  `products`.*, `products_img`.`img1`, `category`.`cat_name`,
				  `subcat`.`sub_name`
				FROM
				  `products` INNER JOIN
				  `products_img` ON `products`.`pid` = `products_img`.`pid` INNER JOIN
				  `category` ON `products`.`cat_id` = `category`.`cat_id` INNER JOIN
				  `subcat` ON `subcat`.`cat_id` = `category`.`cat_id`
				WHERE
				  `products`.`poster` = '".$_SESSION['usr']."'
				GROUP BY
				  `products`.`pid`
				ORDER BY
				  `products`.`p_change_place` DESC, `products`.`p_create` DESC";
				  
$rsdc = mysql_query($sqlc);
$cols = mysql_num_rows($rsdc);
$page = $_REQUEST['page'];

$start = ($page-1)*10;
$sql = "SELECT
		  `products`.*, `products_img`.`img1`, `category`.`cat_name`,
		  `subcat`.`sub_name`
		FROM
		  `products` INNER JOIN
		  `products_img` ON `products`.`pid` = `products_img`.`pid` INNER JOIN
		  `category` ON `products`.`cat_id` = `category`.`cat_id` INNER JOIN
		  `subcat` ON `subcat`.`cat_id` = `category`.`cat_id`
		WHERE
		  `products`.`poster` = '".$_SESSION['usr']."'
		GROUP BY
		  `products`.`pid`
		ORDER BY
		  `products`.`p_change_place` DESC, `products`.`p_create` DESC
		LIMIT $start,10";
$rsd = mysql_query($sql);
?>

<div id="post" style="margin-top: 10px;">                        
<table id="list" width="730" border="0" cellspacing="1" cellpadding="1">
    <thead>
      <tr>
        <td width="20">
            <div align="center">
              <input type="checkbox" onclick="$('input[name*=\'selected\']').attr('checked', this.checked);">
            </div>
        </td>                              	
        <td class="left"><div align="center">รูปภาพ</div></td>
        <td class="right"  width="400"><div align="center">รายการประกาศ</div></td>    
        <td width="64"><div align="center">แก้ไข</div></td>     
        <td><div align="center">เลื่อนประกาศ</div></td>    
        <td width="128"><div align="center">โฆษณา<br/>ปักหมุด</div></td>                   
      </tr>
    </thead>
    <tbody>
    <?php 
    if($cols>0){
		while($show_post = $db->fetch($rsd)){
		?>
			<tr>
				<td>
					<div align="center">
					  <input type="checkbox" name="selected[]" value="<?php echo $show_post['pid'];?>">
					</div>
				</td>
				<td width="47" align="center">
                	<div id="post_img"><img src="<?php echo IMG_PATH.$show_post['img1'];?>" /></div><strong style="color:#0369C0;"><?php echo $show_post['pid'];?></strong>
              	</td>
				<td>
					<div id="post_title" style="width: 400px;">
						<div id="post_header"><a href="<?php echo WEB_URL.'/products/?m=products&p=product_detail&pid='.$show_post['pid'];?>"><?php echo $show_post['p_title'];?></a> <span id="price">฿ <?php ($show_post['p_sure_price'] != 0)? print $show_post['p_sure_price'].'.-': print $show_post['p_price'];?></span></div>
						<span id="post_cat">Category :  <a href="<?php echo WEB_URL.'/products/?m=categories&p=categories&cid='.$show_post['cat_id'].'&c='.$show_post['cat_name'];?>"><?php echo $show_post['cat_name'];?></a></span> | <span id="post_dat"> Date Poste : <?php echo $show_post['p_create'];?></span>            
					</div> 
				</td>                                
				<td>
					<a href="<?php echo WEB_URL;?>/products/?m=post&p=edit_post&pid=<?php echo $show_post['pid'];?>&c=<?php echo $show_post['cat_name'];?>&sc=<?php echo $show_post['sub_name'];?>" id="edit_post" pid="<?php echo $show_post['pid'];?>">
						<img src="../styles/images/notepad.png" width="64" />
					</a>
				</td>
				<td>
					<a href="#" id="move_up" pid="<?php echo $show_post['pid'];?>">
						<img src="../styles/images/up.png" width="64" align="middle" />
					</a>
				</td>
				<td>
					<div align="center">
						<a 
							class="medium button blue" 
							id="sticky" 
							href="#dialog" 
							pid="<?php echo $show_post['pid'];?>"                      
						>ปักหมุด</a>
					</div>
				</td>
			</tr>	
		<?php 
		}
    }else{
    ?>
        <tr>
            <td colspan="6"><div style="color: #0369C0; font-size: 18px; font-weight: bold; padding: 10px; text-align: center;">ยังไม่มีประกาศ</div></td>
        </tr>
    <?php
    }
    ?>
    </tbody>
</table>
</div>
<div style="display: none;">
    <div id="dialog" style="width:600px;height:150px;overflow:auto;">
        <table border="0" cellpadding="2" cellspacing="2" id="list" width="100%">
        	<thead>
            	<tr>
                    <th width="100">รหัสสินค้า</th>                
                    <th width="100">หมวดหมู่</th>
                    <th width="100">ตำแหน่ง</th>
                    <th width="100">ระยะเวลาติดประกาศ</th>
                    <th width="100">ราคา ฿</th>
                <tr>
            </thead>
            <tbody>
            	<tr>
                    <td align="center"><strong><div id="ads_pid"></div></strong></td>
                    <td align="center"><div id="ads_cid"></div></td>
                    <td align="center"><div id="ads_position"></div></td>
                    <td align="center"><div id="ads_period"></div></td>
                    <td>
                        <div id="ads_price">
                            <strong>
                            ราคา <b style="color:red;">1500฿</b><br/>
                            ส่วนลด <b style="color:red;">0%</b><br/>
                            รวม <b style="color:red;">1500฿</b>
                            </strong>
                        </div>
                        <input type="hidden" name="sticky_detail" id="sticky_detail" price="300" discount="0" total="300" />
                    </td>
                <tr>
            </tbody>
        </table>
        <a class="medium button blue" id="post_sticky" href="#" style="margin-top:10px;float:right;">ลงโฆษณา</a>
    </div>
    
    <div id="cat_list">
    <?php
		$csql = 'SELECT * FROM category';
		$cq = mysql_query($csql);
		echo '<select id="catlist">';
		echo '<option value="99">ทุกหมวด</option>';
		while($crs = mysql_fetch_assoc($cq)){
			echo '<option value="'.$crs['cat_id'].'" disabled="disabled">'.$crs['cat_name'].'</option>';
		}
		echo '</select>';

	?>
    </div>
    <div id="position_list">
    <?php
		$psql = 'SELECT * FROM positions LIMIT 9,10';
		
		$pq = mysql_query($psql);
		echo '<select id="postlist">';
		while($prs = mysql_fetch_assoc($pq)){
			//($prs['id']>10 && $prs['id']<20)? print '<option value="'.$prs['id'].'" >'.$prs['position_name'].'</option>':'<option value="'.$prs['id'].'" disabled="disabled">'.$prs['position_name'].'</option>';
			$check_ads_pos 		= 'SELECT * FROM advertise WHERE position_id = '.$prs['id'];
			$check_ads_pos_q 	= mysql_query($check_ads_pos);
			$check_ads_pos_num	= mysql_num_rows($check_ads_pos_q);
			$disabled = ($check_ads_pos_num > 0)?'disabled="disabled"':'';
			echo '<option value="'.$prs['id'].'" '.$disabled.' >'.$prs['position_name'].'</option>';
		}
		echo '</select>';
		mysql_close();
	?>
    </div>
    <div id="period_list">
    	<select id="periodlist">
        	<option value="1">1 month</option>
            <option value="2">2 months</option>
            <option value="3">3 months</option>
            <option value="4">4 months</option>
            <option value="5">5 months</option>
            <option value="6">6 months</option>
            <option value="7">7 months</option>
            <option value="8">8 months</option>
            <option value="9">9 months</option>
            <option value="10">10 months</option>
            <option value="11">11 months</option>
            <option value="12">12 months</option>
        </select>
    </div>
</div>

<script type="text/javascript">
	var pid;
	var cid;
	var pos;
	var per;
	var pos_disabled = true;

	cid = $('#catlist').val();
	pos = $('#postlist').val();
	per = $('#periodlist').val();
	
	$('#ads_cid').html($('#cat_list').html());
	$('#ads_position').html($('#position_list').html());
	$('#ads_period').html($('#period_list').html());
	
	$('a#sticky').fancybox({
		'titlePosition'		: 'outside',
		'overlayColor'		: '#000',
		'overlayOpacity'	: 0.9
	});

	$('a#sticky').bind('click',function(){
		pid = $(this).attr('pid');
		$('#ads_pid').html(pid);
		$.post('../query.php',{check_sticky:true, pid:pid},function(result){
			if(result.status == 1){				
				alert('ไม่สามารถปักหมุดประกาศนี้ซ้ำได้อีกเพราะประกาศนี้ปักหมุดอยู่แล้ว');
				location.reload();
			}else{
				$('#ads_pid').html($(this).attr('pid'));	
				$('input#sticky_detail').attr({'pid':pid, 'cid':cid, 'pos':pos, 'per':per});		
			}
		},'json');
		
	});
	
	$('#postlist').change(function(){
		pos = $(this).val();
		pos_disabled = $(this,'option').attr('disabled');
		$('input#sticky_detail').attr({'pid':pid, 'cid':cid, 'pos':pos, 'per':per});
	});

	$('#catlist').change(function(){
		cid = $(this).val();
		$('input#sticky_detail').attr({'pid':pid, 'cid':cid, 'pos':pos, 'per':per});
	});
	
	$('#periodlist').change(function(){
		per				= $(this).val();		
		var price 		= per*1500;
		var discount 	= (per>=1 && per<3? 0:(per>2 && per<6 ? 5:(per>5 && per<12 ? 10:20)));
		var total		= price - ((price*discount)/100);
		var html		= '<strong>ราคา <b style="color:red;">'+price+'฿</b>&nbsp;&nbsp;&nbsp;&nbsp;\n';
			html		+= ' ส่วนลด <b style="color:red;">'+discount+'%</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n';
			html		+= ' รวม <b style="color:red;">'+total+'฿</b></strong>\n';

		$('input#sticky_detail').attr({'pid':pid, 'cid':cid, 'pos':pos, 'per':per, 'price':price, 'discount':discount, 'total':total});

		$('#ads_price').html(html);

	});
	
	$('#post_sticky').click(function(){		
		var hidden = $(this).closest('div').find('table tbody tr td input#sticky_detail');
		var p 	= hidden.attr('price');
		var d 	= hidden.attr('discount');
		var t	= hidden.attr('total');
		var check_pos_disabled 	= $('#postlist option').attr('disabled');
		//alert(p+'-'+d+'-'+t+'-'+pid+'-'+cid+'-'+pos+'-'+per);
		if((check_pos_disabled&&pos_disabled) && (cid == 99)){ // 99 คือ ทุกหมวด เมื่อเลือกหมวด 99 จะสามารถลงได้แค่  10 ป้ายเพราะจะแสดงทุกหมวด แต่ถ้าลงแยกหมวดจะสามารถลงได้ตำแหน่งละ 33 ประกาศตามจำนวนหมวดหมู่ที่มีคือ 33 หมวดหมู่
			if($('#warning').text() == ""){
				$('#dialog').append('<p id="warning" style="width: 500px;">ไม่สามารถปักหมุดในตำแหน่งนี้ได้ค่ะ!</p>');
			}
		}else{
			$.ajax({
				type	: 'POST',
				url		: '../query.php',
				data	: {sticky_ads:true, price:p, discount:d, total:t, pid:pid, cid:cid, position:pos, period:per},
				dataType: 'json',
				success	:function(msg){
					if(msg.status){
						location.reload();
					}else{
						alert('คุณไม่มีสิทธิ์ลบสินค้านี้ได้');
					}
				}
			});
		}
			
	});
	
</script>
<script type="text/javascript">
$(function(){
	
	$('#move_up',this).click(function(){
		var move_pid = $(this).attr('pid');
		$.post('../query.php', { move_up:true, pid:move_pid }, function(msg){
			msg.status == 1 ? alert('เลื่อนลำดับประกาศสำเร็จแล้ว') : alert('คุณเลื่อนลำดับประกาศได้วันละครั้งค่ะ');
			location.reload();
		},'json');
		return false;
	});
	
	$('#del_all_post').click(function(){
		var query_string = '';
		if(confirm("คุณมั่นใจไหมว่าต้องการลบประกาศนี้")){
			$("#list input[type='checkbox'][name*='selected']").each(function(){
				
				if(this.checked && this.value != 1){
					query_string += "&checkbox_name[]="+ this.value;
				}
				
			});
		
			$.ajax({
				type	: 'POST',
				url		: '../query.php',
				data	: 'delpost=1'+query_string,
				dataType: 'json',
				success	:function(msg){
					if(msg.status){
						location.reload();
					}else{
						alert('คุณไม่มีสิทธิ์ลบสินค้านี้ได้');
					}
				}
			});
		}
		
	});
	
});
</script>